package com.river.boot.biz.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.river.boot.biz.converter.ShopConverter;
import com.river.boot.biz.mapper.ShopMapper;
import com.river.boot.biz.model.entity.Shop;
import com.river.boot.biz.model.form.ShopForm;
import com.river.boot.biz.model.query.ShopQuery;
import com.river.boot.biz.model.vo.ShopVO;
import com.river.boot.biz.service.ShopService;
import com.river.boot.common.exception.BusinessException;
import com.river.boot.common.result.ResultCode;
import com.river.boot.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 店铺管理服务实现类
 *
 * @author JiangCheng Xiang
 * @since 2025-04-24 11:56
 */
@Service
@RequiredArgsConstructor
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    private final ShopConverter shopConverter;

    /**
     * 获取店铺管理分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ShopVO>} 店铺管理分页列表
     */
    @Override
    public IPage<ShopVO> getShopPage(ShopQuery queryParams) {
        Page<ShopVO> pageVO = this.baseMapper.getShopPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取店铺管理表单数据
     *
     * @param id 店铺管理ID
     * @return
     */
    @Override
    public ShopForm getShopFormData(Long id) {
        Shop entity = this.getById(id);
        return shopConverter.toForm(entity);
    }

    /**
     * 新增店铺管理
     *
     * @param formData 店铺管理表单对象
     * @return
     */
    @Override
    public boolean saveShop(ShopForm formData) {
        // 一个商家用户只能关联一个店铺
        if (this.lambdaQuery()
                .eq(Shop::getUserId, formData.getUserId())
                .exists()) {
            throw new BusinessException(ResultCode.SHOP_USER_EXIST);
        }
        Shop entity = shopConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }

    /**
     * 更新店铺管理
     *
     * @param id       店铺管理ID
     * @param formData 店铺管理表单对象
     * @return
     */
    @Override
    public boolean updateShop(Long id, ShopForm formData) {
        Shop entity = shopConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }

    /**
     * 删除店铺管理
     *
     * @param ids 店铺管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteShops(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的店铺管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
